package com.aye.haze_detection;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.qweather.plugin.view.HeContent;
import com.qweather.plugin.view.HorizonView;
import com.qweather.plugin.view.QWeatherConfig;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.QWeather;

import java.util.ArrayList;
import java.util.List;

public class WeatherActivity extends AppCompatActivity {
    private static final String TAG = WeatherActivity.class.getSimpleName();
    String CityId;
    String[] TempMax = new String[15];
    String[] TempMin = new String[15];
    String[] Humidity = new String[15];
    Integer[] Data_TempMax = new Integer[15],Data_TempMin = new Integer[15],Data_Humidity = new Integer[15];
    Button button_refresh,button_tempmax,button_tempmin,button_humidity;
    Boolean a = false;
//    StaticReceiver staticReceiver = new StaticReceiver();
//    IntentFilter filter = new IntentFilter();
    private RecyclerView recyclerView;
    List<Integer> data_max,data_min,data_humidity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);

//        filter.addAction(ActionUtils.ACTION_FLAG);
//        registerReceiver(staticReceiver,filter);
         Intent intent = this.getIntent();
         CityId = intent.getStringExtra("cityid");
        Log.d(TAG, "WeatherActivity CityId: "+CityId);
//        /**
//         * 初始化
//         * @param key   用户的KEY
//         * @param location  城市信息，若不传或为空则调用Android源生定位
//         */
//        QWeatherConfig.init("f667aece7e3146fc90d441a825ad8fe4",CityId);
        QWeather.getWeather15D(WeatherActivity.this, CityId, new QWeather.OnResultWeatherDailyListener() {
            @Override
            public void onError(Throwable throwable) {
                Log.i(TAG, "getWeather15D onError: " + throwable);
            }

            @Override
            public void onSuccess(WeatherDailyBean weatherDailyBean) {
                Log.i(TAG, "getWeather15D onSuccess: " + new Gson().toJson(weatherDailyBean));
                if (Code.OK == weatherDailyBean.getCode()) {
//                    WeatherDailyBean.DailyBean now = (WeatherDailyBean.DailyBean) weatherDailyBean.getDaily();
                      List<WeatherDailyBean.DailyBean> now = weatherDailyBean.getDaily();
                      for (int i=0;i<15;i++) {
                          TempMax[i] = now.get(i).getTempMax();
                          TempMin[i] = now.get(i).getTempMin();
                          Humidity[i] = now.get(i).getHumidity();
                          Log.d(TAG, "TempMax+TempMin+Humidity: "+"第"+(i+1)+"天 "+TempMax[i]+" "+TempMin[i]+" "+Humidity[i]);
                          Data_TempMax[i] = Integer.parseInt(TempMax[i]);
                          Data_TempMin[i] = Integer.parseInt(TempMin[i]);
                          Data_Humidity[i] = Integer.parseInt(Humidity[i]);
                          Log.d(TAG, "TempMax+TempMin+Humidity: "+"第"+(i+1)+"天 "+Data_TempMax[i]+" "+Data_TempMin[i]+" "+Data_Humidity[i]);

                      }

                } else {
                    //在此查看返回数据失败的原因
                    Code code = weatherDailyBean.getCode();
                    Log.i(TAG, "failed code: " + code);
                }
            }
        });



        recyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);




        button_refresh = findViewById(R.id.Button_Refresh);
        button_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data_max = new ArrayList<>();
                data_min = new ArrayList<>();
                data_humidity = new ArrayList<>();
                Log.d(TAG, "recyclerView: "+Data_TempMax[0]);
                a = true;
                if (Data_TempMax[0] !=null){
                    for (int j=0;j<15;j++){

                        data_max.add(Data_TempMax[j]);
                        data_min.add(Data_TempMin[j]);
                        data_humidity.add(Data_Humidity[j]);

                    }


                }

            }
        });
                    button_tempmax = findViewById(R.id.button_tempmax);
                    button_tempmax.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Data_TempMax[0] !=null && a) {
                                Day_Adapter adapter_max = new Day_Adapter(WeatherActivity.this, data_max);
                                recyclerView.setAdapter(adapter_max);
                            }

                        }
                    });

                    button_tempmin = findViewById(R.id.button_tempmin);
                    button_tempmin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Data_TempMax[0] !=null && a){
                                Day_Adapter adapter_min = new Day_Adapter(WeatherActivity.this,data_min);
                                recyclerView.setAdapter(adapter_min);
                            }
                        }
                    });

                    button_humidity = findViewById(R.id.button_humidity);
                    button_humidity.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Data_TempMax[0] !=null && a){
                                Day_Adapter adapter_humidity = new Day_Adapter(WeatherActivity.this,data_humidity);
                                recyclerView.setAdapter(adapter_humidity);
                            }
                        }
                    });










    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
}